package com.anolesoft.yr.mbus.part.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.part.dto.PartModelDTO;
import com.anolesoft.yr.mbus.part.dto.PartModelTreeDynamicQueryDTO;
import com.anolesoft.yr.mbus.part.dto.PartModelTreeQueryDTO;
import com.anolesoft.yr.mbus.part.service.PartModelService;
import com.anolesoft.yr.mbus.part.vo.PartModelTreeDynamicQueryVO;
import com.anolesoft.yr.mbus.part.vo.PartModelTreeQueryVO;
import com.anolesoft.yr.mbus.part.vo.PartModelVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/11 16:16
 */
@RestController
public class PartModelController {
    @Autowired
    private PartModelService partModelService;

//    public static PartModelVO dto2vo(PartModelDTO partModelDTO) {
//        PartTypeDTO partType = partModelDTO.getPartType();
//        PartTypeVO partTypeVO = PartTypeVO.builder()
//                .createTime(partType.getCreateTime())
//                .createUserUid(partType.getCreateUserUid())
//                .deleteFlag(partType.getDeleteFlag())
//                .factoryCode(partType.getFactoryCode())
//                .modifyTime(partType.getModifyTime())
//                .modifyUserUid(partType.getModifyUserUid())
//                .objuid(partType.getObjuid())
//                .remark(partType.getRemark())
//                .typeCode(partType.getTypeCode())
//                .typeName(partType.getTypeName())
//                .build();
//        return PartModelVO.builder()
//                .partType(partTypeVO)
//                .objuid(partModelDTO.getObjuid())
//                .quantity(partModelDTO.getQuantity())
//                .unit(partModelDTO.getUnit())
//                .partSpecification(partModelDTO.getPartSpecification())
//                .partCode(partModelDTO.getPartCode())
//                .partModel(partModelDTO.getPartModel())
//                .partName(partModelDTO.getPartName())
//                .partBrand(partModelDTO.getPartBrand())
//                .partTypeUid(partModelDTO.getPartTypeUid())
//                .factoryCode(partModelDTO.getFactoryCode())
//                .remark(partModelDTO.getRemark())
//                .modifyUserUid(partModelDTO.getModifyUserUid())
//                .createUserUid(partModelDTO.getCreateUserUid())
//                .createTime(partModelDTO.getCreateTime())
//                .deleteFlag(partModelDTO.getDeleteFlag())
//                .modifyTime(partModelDTO.getModifyTime())
//                .build();
//    }
    private PartModelDTO vo2dto(PartModelVO partModelVO, boolean newId) {
        return PartModelDTO.builder()
                .objuid(newId ? CommonUtils.fastUUID() : partModelVO.getObjuid())
                .quantity(partModelVO.getQuantity())
                .unit(partModelVO.getUnit())
                .partSpecification(partModelVO.getPartSpecification())
                .partCode(partModelVO.getPartCode())
                .partModel(partModelVO.getPartModel())
                .viceModel(partModelVO.getViceModel())
                .partName(partModelVO.getPartName())
                .partBrand(partModelVO.getPartBrand())
                .partTypeUid(partModelVO.getPartTypeUid())
                .factoryCode(partModelVO.getFactoryCode())
                .remark(partModelVO.getRemark())
                .modifyUserUid(SecurityUtils.getUserId())
                .createUserUid(SecurityUtils.getUserId())
                .createTime(partModelVO.getCreateTime())
                .deleteFlag(partModelVO.getDeleteFlag())
                .modifyTime(partModelVO.getModifyTime())
                .parentPartModelUid(partModelVO.getParentPartModelUid())
                .build();
    }

    @PostMapping(value = "/api/part/partmodel/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(PartModelVO partModelVO) {
        PartModelDTO partModelDTO = vo2dto(partModelVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, partModelService.create(partModelDTO));
    }


    @PutMapping(value = "/api/part/partmodel/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(PartModelVO PartModelVO) {
        PartModelDTO partModelDTO = vo2dto(PartModelVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, partModelService.modify(partModelDTO));
    }

    @DeleteMapping(value = "/api/part/partmodel/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, partModelService.remove(id));
    }
    @DeleteMapping(value = "/api/part/partmodel/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult removeBatch(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, partModelService.removeBatch(ids));
    }

    @GetMapping(value = "/api/part/partmodel/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<PartModelDTO> partModelDTO = this.partModelService.selectOneById(id);
        if (partModelDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.partModelDTO2VO(partModelDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }

    }

    @GetMapping(value = "/api/part/partmodel/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<PartModelVO> vos = partModelService.selectAll().stream().map(t -> CustomBeanUtils.partModelDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartModelVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partmodel/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
        PageData<List<PartModelDTO>> pageData = partModelService.selectAll(limit, curr);
        List<PartModelVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.partModelDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartModelVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partmodel/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(PartModelVO partModelVO) {
        PartModelDTO partModelDTO = vo2dto(partModelVO, false);
        List<PartModelVO> vos = this.partModelService.selectList(partModelDTO).stream().map(t -> CustomBeanUtils.partModelDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<PartModelVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partmodel/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectListPage(PartModelVO partModelVO) {
        //PartModelDTO partModelDTO = vo2dto(partModelVO, false);
        PartModelDTO partModelDTO = buildConditon(partModelVO);
        Integer limit = partModelVO.getRows();
        Integer curr = partModelVO.getPage();
        PageData<List<PartModelDTO>> pageData = partModelService.selectList(partModelDTO, limit, curr);
        List<PartModelVO> vos = pageData.getItems().stream().map(CustomBeanUtils::partModelDTO2VO).collect(Collectors.toList());
        PageInfo<List<PartModelVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping(value = "/api/part/partmodel/list/combox", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectComboxList(@RequestBody PartModelVO partModelVO) {

        PartModelDTO partModelDTO = PartModelDTO.builder()
                .partSpecification(partModelVO.getPartSpecification())
                .partName(partModelVO.getPartName())
                .partModel(partModelVO.getPartModel())
                .partBrand(partModelVO.getPartBrand())
                .partCode(partModelVO.getPartCode())
                .build();
        Integer limit = partModelVO.getRows();
        Integer curr = partModelVO.getPage();
        PageData<List<PartModelDTO>> pageData = partModelService.selectComboxList(partModelDTO, limit, curr);
        List<PartModelVO> vos = pageData.getItems().stream().map(CustomBeanUtils::partModelDTO2VO).collect(Collectors.toList());
        PageInfo<List<PartModelVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/part/partmodel/list/tree/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult getTree(HttpServletRequest request) {
        List<PartModelTreeQueryDTO> partModelTreeQueryDTOS = partModelService.getTree("-");

        List<PartModelTreeQueryVO> vos = buildVos(partModelTreeQueryDTOS);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, vos);
    }
//    @GetMapping(value = "/api/part/partmodel/list/tree/dynamic/{parentId}", produces = MediaType.APPLICATION_JSON_VALUE)
//    public AjaxResult getDynamicTree(@PathVariable("parentId") String parentId) {
//        List<PartModelTreeDynamicQueryDTO> dynamicTree = partModelService.getDynamicTree(parentId);
//        List<PartModelTreeDynamicQueryVO> vos =  dynamicTree.stream().map(t->
//            PartModelTreeDynamicQueryVO.builder()
//                    .objuid(t.getObjuid())
//                    .parentPartModelUid(t.getParentPartModelUid())
//                    .partBrand(t.getPartBrand())
//                    .partCode(t.getPartCode())
//                    .partModel(t.getPartModel())
//                    .partName(t.getPartName())
//                    .partSpecification(t.getPartSpecification())
//                    .partType(CustomBeanUtils.partTypeDTO2VO(t.getPartType()))
//                    .partTypeUid(t.getPartTypeUid())
//                    .quantity(t.getQuantity())
//                    .unit(t.getUnit())
//                    .hasChild(t.getHasChild())
//                    .id(t.getObjuid())
//                    .name(t.getPartModel())
//                    .text(t.getPartModel())
//                    .state(t.getHasChild()==0?"open":"closed")
//                    .children(new ArrayList<>(10))
//                    .build() ).collect(Collectors.toList());
//        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, vos);
//    }
    @GetMapping(value = "/api/part/partmodel/list/tree/dynamic", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult getDynamicTree2(
            @RequestParam(value="id",required = false) String parentId,
            @RequestParam(value = "rows",required = false) Integer limit,
            @RequestParam(value = "page",required = false) Integer curr
    ) {
        if(parentId == null || "".equals(parentId)){
            parentId = "-";
        }
        if(!"-".equals(parentId)) {
            List<PartModelTreeDynamicQueryDTO> dynamicTree = partModelService.getDynamicTree(parentId);
            List<PartModelTreeDynamicQueryVO> vos = dynamicTree.stream().map(t ->
                    PartModelTreeDynamicQueryVO.builder()
                            .objuid(t.getObjuid())
                            .parentPartModelUid(t.getParentPartModelUid())
                            .partBrand(t.getPartBrand())
                            .partCode(t.getPartCode())
                            .partModel(t.getPartModel())
                            .viceModel(t.getViceModel())
                            .partName(t.getPartName())
                            .partSpecification(t.getPartSpecification())
                            .partType(CustomBeanUtils.partTypeDTO2VO(t.getPartType()))
                            .partTypeUid(t.getPartTypeUid())
                            .quantity(t.getQuantity())
                            .unit(t.getUnit())
                            .hasChild(t.getHasChild())
                            .id(t.getObjuid())
                            .name(t.getPartModel())
                            .text(t.getPartModel())
                            .state(t.getHasChild() == 0 ? "open" : "closed")
                            .children(new ArrayList<>(10))
                            .build()).collect(Collectors.toList());
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, vos);
        }else{
            PageData<List<PartModelTreeDynamicQueryDTO>> pageData = partModelService.getDynamicTreePage(parentId, limit, curr);
            List<PartModelTreeDynamicQueryVO> vos = pageData.getItems().stream().map(t ->
                    PartModelTreeDynamicQueryVO.builder()
                            .objuid(t.getObjuid())
                            .parentPartModelUid(t.getParentPartModelUid())
                            .partBrand(t.getPartBrand())
                            .partCode(t.getPartCode())
                            .partModel(t.getPartModel())
                            .viceModel(t.getViceModel())
                            .partName(t.getPartName())
                            .partSpecification(t.getPartSpecification())
                            .partType(CustomBeanUtils.partTypeDTO2VO(t.getPartType()))
                            .partTypeUid(t.getPartTypeUid())
                            .quantity(t.getQuantity())
                            .unit(t.getUnit())
                            .hasChild(t.getHasChild())
                            .id(t.getObjuid())
                            .name(t.getPartModel())
                            .text(t.getPartModel())
                            .state(t.getHasChild() == 0 ? "open" : "closed")
                            .children(new ArrayList<>(10))
                            .build()).collect(Collectors.toList());
            PageInfo<List<PartModelTreeDynamicQueryVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
        }
    }

    @GetMapping(value = "/api/part/partmodel/list/tree/dynamic/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult getDynamicTree2(@RequestParam(value="id",required = false) String parentId) {
        if(parentId == null || "".equals(parentId)){
            parentId = "-";
        }
        List<PartModelTreeDynamicQueryDTO> dynamicTree = partModelService.getDynamicTree(parentId);
        List<PartModelTreeDynamicQueryVO> vos =  dynamicTree.stream().map(t->
                PartModelTreeDynamicQueryVO.builder()
                        .objuid(t.getObjuid())
                        .parentPartModelUid(t.getParentPartModelUid())
                        .partBrand(t.getPartBrand())
                        .partCode(t.getPartCode())
                        .partModel(t.getPartModel())
                        .viceModel(t.getViceModel())
                        .partName(t.getPartName())
                        .partSpecification(t.getPartSpecification())
                        .partType(CustomBeanUtils.partTypeDTO2VO(t.getPartType()))
                        .partTypeUid(t.getPartTypeUid())
                        .quantity(t.getQuantity())
                        .unit(t.getUnit())
                        .hasChild(t.getHasChild())
                        .id(t.getObjuid())
                        .name(t.getPartModel())
                        .text(t.getPartModel())
                        .state(t.getHasChild()==0?"open":"closed")
                        .children(new ArrayList<>(10))
                        .build() ).collect(Collectors.toList());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, vos);
    }


    private List<PartModelTreeQueryVO> buildVos(List<PartModelTreeQueryDTO> children) {
        return children.stream().map(t -> {
            PartModelTreeQueryVO partModelTreeQueryVO = PartModelTreeQueryVO.builder()
                    .objuid(t.getObjuid())
                    .id(t.getObjuid())
                    .name(t.getPartModel())
                    .parentPartModelUid(t.getParentPartModelUid())
                    .partBrand(t.getPartBrand())
                    .partCode(t.getPartCode())
                    .partModel(t.getPartModel())
                    .viceModel(t.getViceModel())
                    .partName(t.getPartName())
                    .partSpecification(t.getPartSpecification())
                    .partType(CustomBeanUtils.partTypeDTO2VO(t.getPartType()))
                    .partTypeUid(t.getPartTypeUid())
                    .quantity(t.getQuantity())
                    .unit(t.getUnit())
                    .build();
            if (t.getChildren().size() > 0) {
                partModelTreeQueryVO.setChildren(buildVos(t.getChildren()));
            } else {
                partModelTreeQueryVO.setChildren(new ArrayList<>(10));
            }
            return partModelTreeQueryVO;
        }).collect(Collectors.toList());
    }

    private PartModelDTO buildConditon(PartModelVO partModelVO) {
        return PartModelDTO.builder()
                .partTypeUid(CommonUtils.stringNull(partModelVO.getPartTypeUid()))
                .partSpecification(CommonUtils.warpStringNull(partModelVO.getPartSpecification()))
                .partName(CommonUtils.warpStringNull(partModelVO.getPartName()))
                .partModel(CommonUtils.warpStringNull(partModelVO.getPartModel()))
                .viceModel(CommonUtils.warpStringNull(partModelVO.getViceModel()))
                .partBrand(CommonUtils.warpStringNull(partModelVO.getPartBrand()))
                .partCode(CommonUtils.warpStringNull(partModelVO.getPartCode()))
                .objuid(partModelVO.getObjuid())
                .build();
    }
}
